﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Demo.Net.Infrastructure
{
    public static class RandomHelper
    {
        public static int RandomSeed()
        {
            RandomNumberGenerator randomNumberGenerator = RandomNumberGenerator.Create();
            byte[] array = new byte[4];
            randomNumberGenerator.GetBytes(array);
            return BitConverter.ToInt32(array, 0);
        }

        public static int NextInt32(int length)
        {
            return ConvertHelper.ChangeType<int>(NextString(length));
        }

        public static long NextInt64(int length)
        {
            return ConvertHelper.ChangeType<long>(NextString(length));
        }

        public static string NextString(int length)
        {
            string text = string.Empty;
            string[] array = new string[10] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
            Random random = new Random(RandomSeed());
            for (int i = 0; i < length; i++)
            {
                text += array[random.Next(0, array.Length - 1)];
            }

            return text;
        }

        public static int Next()
        {
            return new Random(RandomSeed()).Next();
        }

        public static int Next(int maxValue)
        {
            return new Random(RandomSeed()).Next(maxValue);
        }

        public static int Next(int minValue, int maxValue)
        {
            return new Random(RandomSeed()).Next(minValue, maxValue);
        }

        public static double NextDouble()
        {
            return new Random(RandomSeed()).NextDouble();
        }

        public static void NextBytes(byte[] buffer)
        {
            new Random(RandomSeed()).NextBytes(buffer);
        }
    }
}
